package com.example.detectivedemo.application;

import com.example.detectivedemo.domain.product.Product;
import com.example.detectivedemo.domain.product.ProductService;
import com.example.detectivedemo.infrastructure.product.ProductDao;
import com.example.detectivedemo.utils.LogUtils;

import java.util.List;
import java.util.Optional;

/**
 * 应用服务类
 * 整合所有业务服务，作为UI层和业务层之间的桥梁
 */
public class AppService {
    
    private static AppService instance;
    private ProductService productService;
    
    /**
     * 私有构造函数，实现单例模式
     */
    private AppService() {
        // 初始化各个服务
        initializeServices();
        LogUtils.info("AppService initialized");
    }
    
    /**
     * 获取AppService实例（单例模式）
     * @return AppService实例
     */
    public static synchronized AppService getInstance() {
        if (instance == null) {
            instance = new AppService();
        }
        return instance;
    }
    
    /**
     * 初始化所有服务
     */
    private void initializeServices() {
        // 初始化产品服务
        ProductDao productDao = new ProductDao();
        productService = new ProductService(productDao);
        
        // 可以在此初始化其他服务
    }
    
    /**
     * 获取所有产品
     * @return 产品列表
     */
    public List<Product> getAllProducts() {
        LogUtils.info("AppService: Getting all products");
        return productService.findAll();
    }
    
    /**
     * 根据ID获取产品
     * @param id 产品ID
     * @return 产品对象
     */
    public Optional<Product> getProductById(String id) {
        LogUtils.info("AppService: Getting product by id: " + id);
        return productService.findById(id);
    }
    
    /**
     * 创建产品
     * @param product 产品对象
     * @return 创建的产品
     */
    public Product createProduct(Product product) {
        LogUtils.info("AppService: Creating product: " + product.getName());
        return productService.create(product);
    }
    
    /**
     * 更新产品
     * @param product 产品对象
     * @return 更新后的产品
     */
    public Product updateProduct(Product product) {
        LogUtils.info("AppService: Updating product: " + product.getId());
        return productService.update(product);
    }
    
    /**
     * 删除产品
     * @param id 产品ID
     * @return 删除是否成功
     */
    public boolean deleteProduct(String id) {
        LogUtils.info("AppService: Deleting product: " + id);
        return productService.deleteById(id);
    }
    
    /**
     * 根据分类查询产品
     * @param category 产品分类
     * @return 分类下的所有产品
     */
    public List<Product> getProductsByCategory(String category) {
        LogUtils.info("AppService: Getting products by category: " + category);
        return productService.findByCategory(category);
    }
    
    /**
     * 根据价格范围查询产品
     * @param minPrice 最低价格
     * @param maxPrice 最高价格
     * @return 价格范围内的所有产品
     */
    public List<Product> getProductsByPriceRange(double minPrice, double maxPrice) {
        LogUtils.info("AppService: Getting products by price range: " + minPrice + "-" + maxPrice);
        return productService.findByPriceRange(minPrice, maxPrice);
    }
    
    /**
     * 根据名称模糊查询产品
     * @param name 产品名称关键词
     * @return 匹配的产品列表
     */
    public List<Product> searchProductsByName(String name) {
        LogUtils.info("AppService: Searching products by name: " + name);
        return productService.findByNameLike(name);
    }
    
    /**
     * 减少产品库存
     * @param productId 产品ID
     * @param quantity 减少数量
     * @return 更新后的产品
     */
    public Product reduceProductStock(String productId, int quantity) {
        LogUtils.info("AppService: Reducing stock for product: " + productId + " by " + quantity);
        return productService.reduceStock(productId, quantity);
    }
    
    /**
     * 增加产品库存
     * @param productId 产品ID
     * @param quantity 增加数量
     * @return 更新后的产品
     */
    public Product increaseProductStock(String productId, int quantity) {
        LogUtils.info("AppService: Increasing stock for product: " + productId + " by " + quantity);
        return productService.increaseStock(productId, quantity);
    }
    
    /**
     * 初始化模拟数据
     */
    public void initializeMockData() {
        LogUtils.info("AppService: Initializing mock data");
        
        // 检查是否已有数据
        if (getAllProducts().isEmpty()) {
            // 创建模拟产品数据
            Product product1 = new Product();
            product1.setId("P001");
            product1.setName("华为Mate 60 Pro");
            product1.setDescription("旗舰智能手机，支持卫星通信");
            product1.setPrice(6999.00);
            product1.setStock(100);
            product1.setCategory("手机");
            
            Product product2 = new Product();
            product2.setId("P002");
            product2.setName("小米14");
            product2.setDescription("高性能智能手机，徕卡影像");
            product2.setPrice(4999.00);
            product2.setStock(200);
            product2.setCategory("手机");
            
            Product product3 = new Product();
            product3.setId("P003");
            product3.setName("iPad Pro 12.9");
            product3.setDescription("专业级平板电脑，M2芯片");
            product3.setPrice(8999.00);
            product3.setStock(50);
            product3.setCategory("平板");
            
            Product product4 = new Product();
            product4.setId("P004");
            product4.setName("MacBook Pro 16");
            product4.setDescription("专业级笔记本电脑，M3 Pro芯片");
            product4.setPrice(19999.00);
            product4.setStock(30);
            product4.setCategory("笔记本");
            
            Product product5 = new Product();
            product5.setId("P005");
            product5.setName("AirPods Pro 2");
            product5.setDescription("主动降噪耳机，空间音频");
            product5.setPrice(1899.00);
            product5.setStock(150);
            product5.setCategory("耳机");
            
            // 添加模拟数据
            createProduct(product1);
            createProduct(product2);
            createProduct(product3);
            createProduct(product4);
            createProduct(product5);
            
            LogUtils.info("AppService: Mock data initialized successfully");
        } else {
            LogUtils.info("AppService: Mock data already exists");
        }
    }
}